<?php

namespace app\admin\controller;

use app\admin\model\OperationLog as ModelOperationLog;
use app\common\LogService;

class OperationLog extends Base
{
    public function index()
    {
        return view();
    }
    public function add()
    {
        return view();
    }
    public function list()
    {
        $limit = input('limit');
        $operation_name = input('operation_name');
        $status = input('status');
        $uid = input('user_id');
        $target_id = input('target_id');
        $start_time = input('start_time');
        $end_time = input('end_time');
        $list = ModelOperationLog::order('id desc');
        if (!empty($start_time) && !empty($end_time)) {
            $start = strtotime($start_time);
            $end = strtotime($end_time);
            $list->where('create_time', 'between', [$start, $end]);
        }
        if (!empty($uid)) {
            $list->where(['user_id' => $uid]);
        }
        if (!empty($target_id)) {
            $list->where(['target_id' => $target_id]);
        }
        if (!empty($operation_name)) {
            $list->where('operation_name', 'like', "%$operation_name%");
        }
        if (isset($status) && ($status == 0 || $status == 1)) {
            $list->where(['status' => $status]);
        }
        $list = $list->paginate($limit);
        return success($list);
    }
    public function del($id)
    {
        $recordLog = function ($status, $message = '') use ($id) {
            LogService::record(
                name: '批量删除日志',
                target_id: $id,
                user_id: $this->uid,
                status: $status,
                content: $message == '' ? [] : ['reason' => $message]
                ,user_type : 2

            );
        };
        $ids = explode(',', $id);
        $res = ModelOperationLog::destroy($ids);
        if ($res) {
            $recordLog(1);
            return success();
        }
        $recordLog(0);
        return error();
    }
}
